/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.proto.itemserverprotoVo.openSpriteLocationRequest;
import com.dc.gameserver.proto.itemserverprotoVo.openSpriteLocationResponse;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:14-1-13 </br>
 *         Time:下午4:06 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment：  开启新的灵泉位
 */
@Service
public class openSpriteLocationController extends AbstractController {

    @Resource
    private ModelCacheService modelCacheService;

    @Resource
    private PlayerService playerService;


    @Override
    public void PostConstruct() {
        int ID= openSpriteLocationRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=openSpriteLocationRequest.getDefaultInstance();
    }


    /**
     message openSpriteLocationResponse{
     enum msgID {
     ID = 2020;
     }
     required uint32 openStatus=1;//操作状态  1.开启成功 2. 消耗类道具不足 3. 其他
     optional uint32 location=2;//开启新的位置编号
     }
      灵泉开启位置 20 21 22 23,只记录开启的，推算其他灵泉位置状态
     * @param messageLite     数据载体
     * @param player active object
     * @throws Exception
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        openSpriteLocationResponse.Builder builder=openSpriteLocationResponse.newBuilder();
        int roleID=player.getID();
        String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
        TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);

        /**
         * 判定道具是否足够
         */

        byte tempLocationID=tRole.getLingquanlocation();
        if (tempLocationID==0){
            tempLocationID= 0x14;//首次开启
        }else {
            ++tempLocationID;
        }
        if (tempLocationID<=23){
            //开启成功
            tRole.setLingquanlocation(tempLocationID);

            playerService.updateRole(tRole);
            modelCacheService.addObjectPrefixID(role_prefix,tRole);
            builder.setOpenStatus(0x1);
            builder.setLocation(tempLocationID);
        }
        player.DirectSend(wrappedBufferShort(openSpriteLocationResponse.msgID.ID_VALUE,builder.build()));
    }
}